{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=[\n",
    "    ['A','a',5],\n",
    "    ['A','b',10],\n",
    "    ['A','c',10],\n",
    "    ['A','d',5],\n",
    "    ['B','a',4],\n",
    "    ['B','b',5],\n",
    "    ['B','c',4],\n",
    "    ['B','d',10],\n",
    "    ['C','a',4],\n",
    "    ['C','c',10],\n",
    "    ['C','d',5],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A': {'a': 5, 'b': 10, 'c': 10, 'd': 5},\n",
       " 'B': {'a': 4, 'b': 5, 'c': 4, 'd': 10},\n",
       " 'C': {'a': 4, 'c': 10, 'd': 5}}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data = {}\n",
    "\n",
    "\n",
    "for user, item,record in data:\n",
    "    train_data.setdefault(user,{})\n",
    "    train_data[user][item] = record\n",
    "\n",
    "\n",
    "train_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': {'A', 'B', 'C'},\n",
       " 'b': {'A', 'B'},\n",
       " 'c': {'A', 'B', 'C'},\n",
       " 'd': {'A', 'B', 'C'}}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 建立item_user倒排表\n",
    "item_users = dict()\n",
    "for u, items in train_data.items():\n",
    "    for i in items:\n",
    "        if i not in item_users:\n",
    "            item_users[i] = set()\n",
    "        item_users[i].add(u)\n",
    "item_users"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(user, j):\n",
    "    rating = 0\n",
    "    cnt = 0\n",
    "    if j not in item_users:\n",
    "        return rating\n",
    "    for i in train_data[user]:\n",
    "        if i == j:\n",
    "            continue\n",
    "        if i not in item_users:\n",
    "            continue\n",
    "        common_users = item_users[i] & item_users[j]\n",
    "        if len(common_users) == 0:\n",
    "            continue\n",
    "        dev_ij = 0\n",
    "        cnt += 1\n",
    "        for common_user in common_users:\n",
    "            dev_ij += (train_data[common_user][j] - train_data[common_user][i])\n",
    "        dev_ij /= len(common_users)\n",
    "        rating += (dev_ij + train_data[user][i])\n",
    "    rating /= cnt\n",
    "    return rating\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict('C','b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
